package com.cskaoyan.controller.backgroundsystemcontroller;

import cn.dev33.satoken.stp.StpUtil;
import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.IssueListVO;
import com.cskaoyan.bean.vo.MallBrandListVO;
import com.cskaoyan.bean.vo.MallBrandVO;
import com.cskaoyan.bean.vo.MallRegionListVO;
import com.cskaoyan.bean.vo.grouponvo.CreatIssueVO;
import com.cskaoyan.bean.vo.grouponvo.KeywordListVO;
import com.cskaoyan.dao.LogoutMapper;
import com.cskaoyan.service.MallService;
import com.cskaoyan.utils.StringUtils;
import lombok.SneakyThrows;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.Serializable;
import java.lang.System;
import java.util.List;
import java.util.Map;

/**
 * @author: hdf、ckd
 * @class: MallController
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("admin")
public class MallController {

    @Autowired
    MallService mallService;
    @Autowired
    LogoutMapper logoutMapper;

    /**
     * @author: lds
     * @title: brandList
     * @description: 显示行政区域
     * @param: []
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @RequestMapping("region/list")
    public BaseRespVo regionList() {

        List<MallRegionListVO> mallRegionListVOS = mallService.queryRegion();

        return BaseRespVo.ok(mallRegionListVOS);
    }

    /**
     * @author: hdf
     * @title: brandList
     * @description: 获得品牌制造商列表, 根据ID和名称查询
     * @param:
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @RequestMapping("/brand/list")
    @RequiresPermissions(value = {"*","admin:brand:list"},logical = Logical.OR)
    public BaseRespVo brandList(BrandListBO brandListBO) {

        MallBrandVO mallBrandVO = mallService.selectByExample(brandListBO);
        return BaseRespVo.ok(mallBrandVO);

    }

    /**
     * @param brandAddBO
     * @return
     * @author: hdf
     * @description: 添加品牌商
     */
    @PostMapping("/brand/create")
    @RequiresPermissions(value = {"*","admin:brand:create"},logical = Logical.OR)
    public BaseRespVo brandCreate(@RequestBody BrandAddBO brandAddBO) {

        MallBrandCreateVO mallBrandCreateVO = mallService.insertSelective(brandAddBO);
        if (mallBrandCreateVO == null){
            return BaseRespVo.fail("品牌商不能重复添加！");
        }
        return BaseRespVo.ok(mallBrandCreateVO);
    }

    /**
     * @param brandAddBO
     * @return
     * @author: hdf
     * @description: 删除品牌商, 逻辑删除，在数据库中将deleted改为1，表示删除。
     */
    @PostMapping("/brand/delete")
    @RequiresPermissions(value = {"*","admin:brand:delete"},logical = Logical.OR)
    public BaseRespVo brandDelete(@RequestBody Brand brand) {
        mallService.updateByExampleSelective(brand);
        return BaseRespVo.ok();
    }


    /**
     * @param brand
     * @return
     * @author: hdf
     * @description: 修改品牌商
     */
    @PostMapping("/brand/update")
    @RequiresPermissions(value = {"*","admin:brand:update"},logical = Logical.OR)
    public BaseRespVo brandUpdate(@RequestBody Brand brand) {
        Brand brand1 = mallService.updateByExample(brand);
        return BaseRespVo.ok(brand);
    }


    /**
     * @param
     * @return
     * @author: hdf
     * @description: 显示所有商品类目
     */
    @RequiresPermissions(value = {"*","admin:category:list"},logical = Logical.OR)
    @GetMapping("/category/list")
    public BaseRespVo categoryList() {
        List<CategoryListVO> categoryListVOList = mallService.selectCategory();
        return BaseRespVo.ok(categoryListVOList);
    }

    /**
     * @param
     * @return
     * @author: hdf
     * @description: 显示类目L1
     */
    @RequiresPermissions(value = {"*","admin:category:list"},logical = Logical.OR)
    @GetMapping("/category/l1")
    public BaseRespVo categoryListWithL1() {
        List<CategoryL1VO> categoryL1VOList = mallService.selectL1Category();
        return BaseRespVo.ok(categoryL1VOList);
    }

    /**
     * @param
     * @return
     * @author: hdf
     * @description: 添加类目
     */
    @RequiresPermissions(value = {"*","admin:category:create"},logical = Logical.OR)
    @PostMapping("/category/create")
    public BaseRespVo categoryCreate(@RequestBody CategoryAddBO categoryAddBO) {
        if ("L2".equals(categoryAddBO.getLevel()) && categoryAddBO.getPid() == 0) {
            return BaseRespVo.fail("请先添加一级类目！");
        }
        CategoryAddVO category = mallService.createCategory(categoryAddBO);
        if (category == null) {
            return BaseRespVo.fail("商品类目不允许重复！");
        }
        return BaseRespVo.ok(category);
    }

    /**
     * @param categoryL1UpdateBO
     * @return
     * @author: hdf
     * @description: 编辑类目
     */
    @RequiresPermissions(value = {"*","admin:category:update"},logical = Logical.OR)
    @PostMapping("/category/update")
    public BaseRespVo categoryUpdate(@RequestBody CategoryL1UpdateBO categoryL1UpdateBO){
        String s = mallService.updateL1Category(categoryL1UpdateBO);
        if ("0".equals(s)){
            return BaseRespVo.fail("商品类目不允许重复！");
        }
        return BaseRespVo.ok();
    }

    /**
     * @param categoryL1UpdateBO
     * @return
     * @author: hdf
     * @description: 删除类目
     */
    @RequiresPermissions(value = {"*","admin:category:delete"},logical = Logical.OR)
    @PostMapping("/category/delete")
    public BaseRespVo categoryDelete(@RequestBody CategoryL1UpdateBO categoryL1UpdateBO) {
        mallService.deleteCategory(categoryL1UpdateBO);
        return BaseRespVo.ok();
    }


    /**
     * @author: ckd
     * @title: issue/list
     * @description:通用问题列表list及增删查改接口
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @RequiresPermissions(value = {"*","admin:issue:list"},logical = Logical.OR)
    @RequestMapping("issue/list")
    public BaseRespVo issueList(Integer page, Integer limit, String sort, String order, String question) {
        IssueListVO issueListVO = mallService.issueList(page, limit, sort, order, question);
        return BaseRespVo.ok(issueListVO, "成功");
    }
    @RequiresPermissions(value = {"*","admin:issue:delete"},logical = Logical.OR)
    @RequestMapping("issue/delete")
    public BaseRespVo deleteIssue(@RequestBody Map map) {
        int id = (int) map.get("id");
        mallService.deleteIssue(id);
        return BaseRespVo.ok();
    }
    @RequiresPermissions(value = {"*","admin:issue:update"},logical = Logical.OR)
    @RequestMapping("issue/update")
    public BaseRespVo updateIssue(@RequestBody UpdateIssueBO updateIssueBO) {
        UpdateIssueVO updateIssueVO = mallService.updateIssue(updateIssueBO);
        return BaseRespVo.ok(updateIssueVO, "成功");
    }
    @RequiresPermissions(value = {"*","admin:issue:create"},logical = Logical.OR)
    @RequestMapping("issue/create")
    public BaseRespVo createIssue(@RequestBody Map map) {
        String answer = (String) map.get("answer");
        String question = (String) map.get("question");
        CreatIssueVO creatIssueVO = mallService.createIssue(answer, question);
        return BaseRespVo.ok(creatIssueVO, "成功");
    }


    /**
     * @author: ckd
     * @title: issue/list
     * @description:关键字list及增删查改接口
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @RequiresPermissions(value = {"*","admin:keyword:list"},logical = Logical.OR)
    @RequestMapping("keyword/list")
    public BaseRespVo keywordList(Integer page, Integer limit, String sort, String order, String question, String keyword, String url) {
        KeywordListVO keywordListVO = mallService.keywordList(page, limit, sort, order, keyword, url);
        return BaseRespVo.ok(keywordListVO, "成功");
    }
    @RequiresPermissions(value = {"*","admin:keyword:create"},logical = Logical.OR)
    @RequestMapping("keyword/create")
    public BaseRespVo createKeyword(@RequestBody Map map) {
        Boolean isDefault = (Boolean) map.get("isDefault");
        Boolean isHot = (Boolean) map.get("isHot");
        String keyword = (String) map.get("keyword");
        String url = (String) map.get("url");
        CreatKeyWordVO creatKeyWordVO = mallService.createKeyword(isDefault, isHot, keyword, url);
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value = {"*","admin:keyword:delete"},logical = Logical.OR)
    @RequestMapping("keyword/delete")
    public BaseRespVo deleteKeyword(@RequestBody Map map) {
        int id = (int) map.get("id");
        mallService.deleteKeyword(id);
        return BaseRespVo.ok();
    }
    @RequiresPermissions(value = {"*","admin:keyword:update"},logical = Logical.OR)
    @RequestMapping("keyword/update")
    public BaseRespVo updateKeyword(@RequestBody UpdateKeywordVO keyword) {
        UpdateKeywordVO updateKeyword = mallService.updateKeyword(keyword);
        return BaseRespVo.ok(updateKeyword, "成功");
    }


    @RequestMapping("order/list")
    public BaseRespVo getOrderList(OrderListParamBo orderListParamBo) {

        OrderListVo orderListVo = mallService.getOrderList(orderListParamBo);

        return BaseRespVo.ok(orderListVo, "成功");
    }

    @RequestMapping("order/detail")
    public BaseRespVo getOrderDetail(Integer id) {

        OrderDetailVo orderDetailVo = mallService.getOrderDetail(id);

        return BaseRespVo.ok(orderDetailVo, "成功");
    }

    @RequestMapping("order/ship")
    public BaseRespVo orderShip(@RequestBody OrderShipBO shipBO) {

        try {
            mallService.giveAShip(shipBO);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.fail("发货失败！");
        }
        return BaseRespVo.ok();
    }

    @PostMapping("order/reply")
    public BaseRespVo orderReply(@RequestBody GoodsCommentReplyBo bo) {
        if (bo != null && !StringUtils.isEmpty(bo.getContent())) {
            return BaseRespVo.ok("回复成功");
        }

        return BaseRespVo.fail("回复内容不能为空");
    }
}
